/* ----------------------------------------------------------------------------------------------------------------
 *  SoulManager
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.idea 
{
	import pure.engine.entity.Entity;
	import pure.engine.system.ns_despair;
	use namespace ns_despair;
	
	/**
	 * 魂管理器
	 * [Author] - PURE
	 */
public class SoulManager 
{
	
	
	/**
	 * 获取同一名字的魂列表
	 * 
	 * [Param] - name
	 */
	ns_despair static function getSoulsByName(name:String):Array 
	{
		if (m_soulGroup[name]) 
		{
			return m_soulGroup[name].concat();
		}
		return [];
	}
	
		
	/**
	 * 获取绑定到实体上的全部魂
	 */
	ns_despair static function getSoulsByEntity(E:Entity):Array 
	{
		var l:int;
		var result:Array;
		
		result = [];
		for (l = m_soulProp.length - 1; l >= 0; l--)
		{
			if (m_soulProp[l].m_id == E.m_id)
			{
				result.push(m_soulProp[l]);
			}
		}
		return result;
	}
	
	
	/**
	 * 实体是否有绑定魂
	 * 
	 * [Param] - E
	 */
	ns_despair static function hasSoul(E:Entity):Boolean 
	{
		var l:int;
		
		for (l = m_soulProp.length - 1; l >= 0; l--)
		{
			if (m_soulProp[l].m_id == E.m_id)
			{
				return true;
			}
		}
		return false;
	}
	
	
	/**
	 * 为实体释放全部绑定的魂
	 * 
	 * [Param] - E
	 */
	ns_despair static function releaseAllSouls(E:Entity):void
	{
		var l:int;
		
		for (l = m_soulProp.length - 1; l >= 0; l--)
		{
			if (m_soulProp[l].m_id == E.m_id)
			{
				m_soulProp[l].empty();
			}
		}
	}
	
	
	/**
	 * 注册
	 */
	ns_despair static function register(soul:Soul):void
	{
		if (!m_soulGroup[soul.m_name])
		{
			m_soulGroup[soul.m_name] = [];
		}
		m_soulGroup[soul.m_name].push(soul);
		
		m_soulProp.push(soul);
	}
	
	
	/**
	 * 注销
	 */
	ns_despair static function unregister(soul:Soul):void
	{
		var group:Array;
		
		group = m_soulGroup[soul.m_name];
		group.splice(group.indexOf(soul), 1);
		
		m_soulProp.splice(m_soulProp.indexOf(soul), 1);
	}
	

	
	
	
	ns_despair static var m_soulGroup:Object = { };  // 魂组
	
	ns_despair static var m_soulProp:Vector.<Soul> = new Vector.<Soul>();  // 魂列表
	
}

}